# parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

# generate different encoding paramethers

str="
xrnlib-cli \
    --encoding-conf -compiled-param -password-str \"AlicePassword0#\" \
    --dump-encoding-param -encoding-param $output_dir/alice_encoding_param_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --encoding-conf -compiled-param -password-str \"BobPassword0#\" \
    --dump-encoding-param -encoding-param $output_dir/bob_encoding_param_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --encoding-conf -compiled-param -password-str \"OneTimePassword0#\" \
    --dump-encoding-param -encoding-param $output_dir/otp_encoding_param_file.xf
"
echo ""; echo $str; echo ""
eval $str

# convert datastructures for the encoding paramethers

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $matrix_decompressed_file -to-encode $output_dir/alice_xoron_matrix.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $matrix_decompressed_file -to-encode $output_dir/bob_xoron_matrix.xf
"
echo ""; echo $str; echo ""
eval $str

# generate alice data structures

str="
xrnlib-cli \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/alice_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix.xf \
    --permutation-conf -arch $architecture $permutation_str \
    --make-start-point -start-point $output_dir/alice_start_point_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --arithmetic-conf -start-point $output_dir/alice_start_point_file.xf -monomial-key $output_dir/alice_monomial_key_file.xf \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix.xf \
    --permutation-conf -arch $architecture \
    --make-binomial-key -binomial-key $output_dir/alice_binomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

# send the file to bob and extracting meta data

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/alice_binomial_key_file.xf -to-encode $output_dir/otp_alice_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/otp_alice_binomial_key_file.xf -to-encode $output_dir/alice_binomial_key_file_for_bob.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --permutation-conf -arch 128 \
    --logging-conf -lib-error-log $output_dir/print_alice_binomial_key.err -lib-info-log $output_dir/print_alice_binomial_key.info \
    --print-header -xrn-file $output_dir/alice_binomial_key_file_for_bob.xf ; \
if grep -q XRN-ERR \"$output_dir/print_alice_binomial_key.err\" ; then \
    rm $output_dir/print_alice_binomial_key.err ; \
    rm $output_dir/print_alice_binomial_key.info ; \
    xrnlib-cli \
        --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
        --permutation-conf -arch 256 \
        --logging-conf -lib-error-log $output_dir/print_alice_binomial_key.err -lib-info-log $output_dir/print_alice_binomial_key.info \
        --print-header -xrn-file $output_dir/alice_binomial_key_file_for_bob.xf ; \
fi
"
echo ""; echo $str; echo ""
eval $str

bob_architecture=$(cat $output_dir/print_alice_binomial_key.info | grep nbits | sed 's/^.*:.// ; s/.$//')
bob_permutation_used=$(cat $output_dir/print_alice_binomial_key.info | grep permutation_used | sed 's/^.*:.//')

bob_permutation_str=""

echo "# bob_architecture $bob_architecture bob_permutation_used $bob_permutation_used bob_permutation_str=$bob_permutation_str"

# bob generating binomial key

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --arithmetic-conf -binomial-key $output_dir/alice_binomial_key_file_for_bob.xf \
    --permutation-conf -arch $bob_architecture \
    --convert-binomial-key-to-start-point -start-point $output_dir/bob_start_point_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --permutation-conf -arch $bob_architecture $bob_permutation_str \
    --make-monomial-key -monomial-key $output_dir/bob_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --arithmetic-conf -start-point $output_dir/bob_start_point_file.xf -monomial-key $output_dir/bob_monomial_key_file.xf \
    --sequence-conf -xmatrix $output_dir/bob_xoron_matrix.xf \
    --permutation-conf -arch $bob_architecture \
    --make-binomial-key -binomial-key $output_dir/bob_binomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

# exchange key for alice

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/bob_binomial_key_file.xf -to-encode $output_dir/otp_bob_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/otp_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --decode-encode -to-decode $output_dir/otp_bob_binomial_key_file.xf -to-encode $output_dir/bob_binomial_key_file_for_alice.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --arithmetic-conf -binomial-key $output_dir/alice_binomial_key_file.xf -monomial-key $output_dir/alice_monomial_key_file.xf \
    --sequence-conf -xmatrix $output_dir/alice_xoron_matrix.xf \
    --permutation-conf -arch $architecture \
    --exchange-monomial-key -external-binomial-key $output_dir/bob_binomial_key_file_for_alice.xf -shared-monomial-key $output_dir/alice_shared_monomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# exchange key for bob

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --encoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --arithmetic-conf -binomial-key $output_dir/bob_binomial_key_file.xf -monomial-key $output_dir/bob_monomial_key_file.xf \
    --sequence-conf -xmatrix $output_dir/bob_xoron_matrix.xf \
    --permutation-conf -arch $architecture \
    --exchange-monomial-key -external-binomial-key $output_dir/alice_binomial_key_file_for_bob.xf -shared-monomial-key $output_dir/bob_shared_monomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# convert and compare shared keys to a plain text json files

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/alice_encoding_param_file.xf -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/alice_shared_monomial_key_file.json \
    --print-header -xrn-file $output_dir/alice_shared_monomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -encoding-param $output_dir/bob_encoding_param_file.xf -no-password \
    --permutation-conf -arch $bob_architecture \
    --logging-conf -lib-info-log $output_dir/bob_shared_monomial_key_file.json \
    --print-header -xrn-file $output_dir/bob_shared_monomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/alice_shared_monomial_key_file.json $output_dir/bob_shared_monomial_key_file.json && echo 'PASS' || echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

